package com.risenb.service.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.risenb.bean.Manager;
import com.risenb.bean.Page;
import com.risenb.bean.PremissionItem;
import com.risenb.constant.DateFormat;
import com.risenb.dao.ManagerMapper;
import com.risenb.dao.ModuleMapper;
import com.risenb.service.ManagerService;
import com.risenb.util.DateUtil;
import com.risenb.util.ExcelUtil;

@Service
public class ManagerServiceImpl implements ManagerService {
	
	@Autowired
	ManagerMapper managerMapper;
	
	@Autowired
	ModuleMapper moduleMapper;

	@Override
	public int createManager(Manager record) {
		return managerMapper.createManager(record);
	}

	@Override
	public int modifyManagerById(Manager record) {
		return managerMapper.modifyManagerById(record);
	}

	@Override
	public List<Manager> queryByPage(Page<Manager> page) {
		List<Manager> man = managerMapper.queryByPage(page);
		for (int i = 0; i < man.size(); i++) {
			man.get(i).setOperatorM(managerMapper.queryManagerById(man.get(i).getOperator()));
		}
		return man;
	}

	@Override
	public Manager queryManager(Manager manager) {
		Manager m = managerMapper.queryManager(manager);
		return m;
	}

	@Override
	public Manager queryManagerTwo(Manager manager) {
		Manager m = managerMapper.queryManagerTwo(manager);
		return m;
	}

	@Override
	public int removeManagerByIds(List<String> ids) {
		return managerMapper.removeManagerByIds(ids);
	}

	@Override
	public Manager queryManagerById(Integer id) {
		Manager manager = managerMapper.queryManagerById(id);
		return manager;
	}

	@Override
	public int queryCountByIdKey(String idkey) {
		return managerMapper.queryCountByIdKey(idkey);
	}

	@Override
	public void exportExcel(List<Manager> managers, String[] titles, ServletOutputStream outputStream) {
		XSSFWorkbook workBook = new XSSFWorkbook();
		// 在workbook中添加一个sheet,对应Excel文件中的sheet
		XSSFSheet sheet = workBook.createSheet("操作人员信息记录");
		ExcelUtil exportUtil = new ExcelUtil(workBook, sheet);
		XSSFCellStyle headStyle = exportUtil.getHeadStyle();
		XSSFCellStyle bodyStyle = exportUtil.getBodyStyle();
		// 构建表头
		XSSFRow headRow = sheet.createRow(0);
		XSSFCell cell = null;
		for (int i = 0; i < titles.length; i++) {
			cell = headRow.createCell(i);
			cell.setCellStyle(headStyle);
			cell.setCellValue(titles[i]);
		}
		// 构建表体数据
		if (managers != null && managers.size() > 0) {
			for (int j = 0; j < managers.size(); j++) {
				XSSFRow bodyRow = sheet.createRow(j + 1);
				Manager m = managers.get(j);

				cell = bodyRow.createCell(0);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(m.getIdkey());

				cell = bodyRow.createCell(1);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(m.getUsername());

				cell = bodyRow.createCell(2);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(m.getName());

				cell = bodyRow.createCell(3);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(m.getPhone());

				cell = bodyRow.createCell(4);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(DateUtil.format(m.getCreatetime(), DateFormat.DEFAULT_FORMAT));

				cell = bodyRow.createCell(5);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(DateUtil.format(m.getOperatime(), DateFormat.DEFAULT_FORMAT));

				cell = bodyRow.createCell(6);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(m.getStatus().equals("0") ? "启用" : "禁用");

			}
		}
		try {
			workBook.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<Manager> queryAll() {
		return managerMapper.queryAlls();
	}

	@Override
	public List<Manager> queryAll(List<String> expids) {
		return managerMapper.queryAll(expids);
	}

	@Override
	public List<PremissionItem> queryPremission(String username) {
		return managerMapper.queryPremission(username);
	}

}